package lightrain.code.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;

/**
 * @author yue571041524@qq.com
 */
@SuppressWarnings("unchecked")
public class Attr {
	
	private Object object;
	
	public Integer toInt(){
		try {
			return Integer.parseInt(String.valueOf(object));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return null;
	}

	
	public Long toLong(){
		try {
			return Long.parseLong(String.valueOf(object));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public String toString() {
		return String.valueOf(object);
	}
	
	public NObject<?> toNMap(){
		return (NObject<?>) object;
	}
	
	public NArray<?> toNArray(){
		return (NArray<?>) object;
	}
	
	public Date toDate(){
		return (Date) object;
	}
	
	public String toDateStr(String pattern){
		return new SimpleDateFormat(pattern).format(toDate());
	}
	
	
	public Map<String, Object> jsonToMap(){
		return jsonTo(Map.class);
	}
	
	public <T>  T jsonTo(Class<T> type){
		return JSON.parseObject(object.toString(), type);
	}
	
	public <T> List<T> jsonToArray(Class<T> classType){
		return JSON.parseArray(toString(),classType);
	}
	
	@SuppressWarnings("rawtypes")
	public List<Map<String, Object>> jsonToArray(){
		List<Map> array = jsonToArray(Map.class);
		 ArrayList<Map<String,Object>> arrayList = new ArrayList<Map<String,Object>>();
		 for (Map<String, Object> map : array) {
			arrayList.add(map);
		}
		 return arrayList;
	}
	
	public <T> T parse(Class<T> type){
		try {
			if(StringUtils.isEmpty(object))
				return null;
			if(type.equals(object.getClass()))
				return (T) object;
			if(type.equals(String.class))
				return (T) String.valueOf(object);
			if (type.equals(Integer.class) || type.equals(int.class))
				return (T) Integer.valueOf(object.toString());
			if(type.equals(Long.class) || type.equals(long.class))
				return (T) Long.valueOf(object.toString());
			if(type.equals(Double.class) || type.equals(double.class))
				return (T) Double.valueOf(object.toString());
			if(type.equals(Short.class) || type.equals(short.class))
				return (T) Short.valueOf(object.toString());
			if(type.equals(Byte.class) || type.equals(byte.class))
				return (T) Byte.valueOf(object.toString());
			if(type.equals(Float.class) || type.equals(float.class))
				return (T) Float.valueOf(object.toString());
			if(type.equals(Double.class) || type.equals(double.class))
				return (T) Double.valueOf(object.toString());
		} catch (Exception e) {
			new RuntimeException(e);
		}
		return null;
	}

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}

	public Attr(Object object) {
		super();
		this.object = object;
	}
	
	public boolean isEmpty(){
		return StringUtils.isEmpty(object);
	}
	
	public boolean notEmpty(){
		return !isEmpty();
	}
}
